<?php

namespace app\controller\api\controller;

use app\service\CategoryService;
use app\support\ResponseHelper;
use DI\Attribute\Inject;
use Shayvmo\WebmanAnnotations\Annotations\DeleteMapping;
use Shayvmo\WebmanAnnotations\Annotations\GetMapping;
use Shayvmo\WebmanAnnotations\Annotations\PostMapping;
use Shayvmo\WebmanAnnotations\Annotations\PutMapping;
use Shayvmo\WebmanAnnotations\Annotations\RestController;
use support\Response;
use Webman\Http\Request;

#[RestController("/api/category")]
class CategoryController
{
    #[Inject]
    protected  CategoryService $categoryService;

    #[GetMapping("/page")]
    public function page(Request $request): Response
    {
        $list = $this->categoryService->listOf($request->all());
        return ResponseHelper::successJson($list->toArray());
    }

    #[PostMapping]
    public function add(Request $request): Response
    {
        $list = $this->categoryService->save($request->all());
        return ResponseHelper::successJson();
    }

    #[DeleteMapping]
    public function delete(Request $request): Response
    {
        $this->categoryService->deleteById($request->get("id"));
        return ResponseHelper::successJson();
    }

    #[PutMapping]
    public function update(Request $request): Response
    {
        $this->categoryService->update($request->all());
        return ResponseHelper::successJson();
    }

    #[PostMapping("/status/{status}")]
    public function startOrStop(string $status, Request $request): Response
    {
        $this->categoryService->startOrStop($status, $request->get("id"));
        return ResponseHelper::successJson();
    }

    #[GetMapping("/list")]
    public function list(Request $request): Response
    {
        $category = $this->categoryService->getListByType($request->get("type"));
        return ResponseHelper::successJson($category);
    }
}